//
//  ViewController.m
//  TestByFF
//
//  Created by hanxifeng on 16/4/28.
//  Copyright © 2016年 haixu. All rights reserved.
//

#import "ViewController.h"
#import "FBtnArrow.h"
#import "ChatListViewController.h"
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    NSString *token = @"xPREB1LTSt9PI1XKLQKPC8U0DMVkRM9GXlXSENM1aTTg3c3L7Z8YHyp7zc6SXqP83/XvYjbBGEKPXmbosVJyWA==";
    [[RCIM sharedRCIM] connectWithToken:token success:^(NSString *userId) {
        //设置用户信息提供者,页面展现的用户头像及昵称都会从此代理取
        [[RCIM sharedRCIM] setUserInfoDataSource:self];
        NSLog(@"Login successfully with userId: %@.", userId);
        dispatch_async(dispatch_get_main_queue(), ^{
            ChatListViewController *chatListViewController = [[ChatListViewController alloc]init];
            [self.navigationController pushViewController:chatListViewController animated:YES];
        });

    } error:^(RCConnectErrorCode status) {
        NSLog(@"登陆的错误码为:%ld", status);
    } tokenIncorrect:^{
        //token过期或者不正确。
        //如果设置了token有效期并且token过期，请重新请求您的服务器获取新的token
        //如果没有设置token有效期却提示token错误，请检查您客户端和服务器的appkey是否匹配，还有检查您获取token的流程。
        NSLog(@"token错误");
    }];
}

#pragma mark - request
#pragma mark AFN上传图片
-(void)buildUploadPhotoBtn{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 64, 100, 100)];
    btn.backgroundColor = [UIColor redColor];
    [btn addTarget:self action:@selector(photoAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}
-(void)httpRequstUpload:(NSData *)fileData{
    [SVProgressHUD showProgress:0 status:@"正在上传.."];
    NSString *urlStr = @"http://wngs.test.xuhaisoft.com/upgradeapi/userinfo.php";
    NSDictionary *paramsDic = [[NSDictionary alloc] initWithObjectsAndKeys:@"817",@"uid", nil];
    NSDictionary *filesDic = [[NSDictionary alloc] initWithObjectsAndKeys:fileData,@"head", nil];
    [HTTPRequestTool UploadPhoto:urlStr params:paramsDic files:filesDic progress:^(NSProgress *progress,float progressFloat) {
        NSLog(@"百分比%f",progressFloat);
        [SVProgressHUD showProgress:progressFloat status:@"正在上传.."];
    } success:^(id JSON, NSString *responseString) {
        [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
    }];
}

#pragma mark - delegate
#pragma mark - imagepicker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    UIImage *selectImage = [info objectForKey:UIImagePickerControllerEditedImage];
    NSData *imgData = UIImageJPEGRepresentation(selectImage,0.5);
    [self httpRequstUpload:imgData];
}
#pragma mark - action
#pragma mark click action
-(void)photoAction{
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    //        imagePickerController.showsCameraControls = YES;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:imagePickerController animated:YES completion:^{}];
}
-(void)btnAction:(UIButton *)sender{
    [sender setTitle:@"点击了哦" forState:UIControlStateNormal];
}
#pragma mark local action
#pragma mark 左侧是文字 右侧是箭头 带边框的按钮
-(void)buildBtnArrow{
    FBtnArrow *btn = [[FBtnArrow alloc] initWithFrame:CGRectMake(10, 64, self.view.frame.size.width - 20, 45)];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"带箭头的按钮" forState:UIControlStateNormal];
    [btn setImage: [UIImage imageNamed:@"Arrow_right"] forState:UIControlStateNormal];
    [self.view addSubview:btn];
}
#pragma mark 图片压缩
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size {
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0,0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    //返回新的改变大小后的图片
    return scaledImage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
